package com.sdry.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sdry.model.hpf.CacheArea;
import com.sdry.model.hpf.Part;
import com.sdry.service.hpf.CacheAreaService;
import com.sdry.service.hpf.LineSideLibraryService;
import com.sdry.service.hpf.PartService;
 
public class ApplicationContextHelper implements ApplicationContextAware {
 
    private static ApplicationContext appCtx;
    
    private static PartService partService;
    
    private static CacheAreaService cacheAreaService;
    
    private static LineSideLibraryService lineSideLibraryService;
    static {
    	appCtx = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
    	partService = (PartService) appCtx.getBean("partService");
    	cacheAreaService = (CacheAreaService) appCtx.getBean("cacheAreaService");
    	lineSideLibraryService = (LineSideLibraryService) appCtx.getBean("lineSideLibraryService");
    }
    @Override
    public void setApplicationContext( ApplicationContext applicationContext ) throws BeansException {
        appCtx = applicationContext;
    }
 
    public static Object getBean( String beanName ) {
        return appCtx.getBean( beanName );
    }
 
    public static <T> T getBean(Class<T> clz) {
        return (T)appCtx.getBean(clz);
    }
    
    public static void test(String data){
    	String[] str1 = data.split(";");
    	System.out.println(str1.length);
    	for (int i = 0; i < str1.length; i++) {
    		String[] str2 = str1[i].split(",");
    		Part part = partService.findPartByPanum(str2[1]);
			if ("0".equals(str2[4])) {
				lineSideLibraryService.insert(part.getPaid(), str2[2], str2[3], str2[4]);
			} else {
				lineSideLibraryService.insert(part.getPaid(), str2[2], str2[3], str2[4]);
			}
			/*if ("1".equals(str2[0])) {
				
			} else {
				Part part = partService.findPartByPanum(str2[1]);
				cacheAreaService.insert(part.getPaid(), str2[2], Integer.parseInt(str2[3]));
				System.out.println(str2[0]);
				if ("2".equals(str2[0])) {
					System.out.println("====================");
					try {
						(new SerialComm4MainWindows()).connect("COM3");
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}*/
		}
    }
}